package cn.iocoder.yudao.module.biz.controller.admin.customer;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.customer.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.CustomerArchiveDO;
import cn.iocoder.yudao.module.biz.service.customer.CustomerArchiveService;

@Tag(name = "管理后台 - 客户档案")
@RestController
@RequestMapping("/san/biz-customer-archive")
@Validated
public class CustomerArchiveController {

    @Resource
    private CustomerArchiveService customerArchiveService;

    @PostMapping("/create")
    @Operation(summary = "创建客户档案")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:create')")
    public CommonResult<Long> createBizCustomerArchive(@Valid @RequestBody CustomerArchiveSaveReqVO createReqVO) {
        return success(customerArchiveService.createBizCustomerArchive(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户档案")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:update')")
    public CommonResult<Boolean> updateBizCustomerArchive(@Valid @RequestBody CustomerArchiveSaveReqVO updateReqVO) {
        customerArchiveService.updateBizCustomerArchive(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:delete')")
    public CommonResult<Boolean> deleteBizCustomerArchive(@RequestParam("id") Long id) {
        customerArchiveService.deleteBizCustomerArchive(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户档案")
                @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:delete')")
    public CommonResult<Boolean> deleteBizCustomerArchiveList(@RequestParam("ids") List<Long> ids) {
        customerArchiveService.deleteBizCustomerArchiveListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:query')")
    public CommonResult<CustomerArchiveRespVO> getBizCustomerArchive(@RequestParam("id") Long id) {
        CustomerArchiveDO bizCustomerArchive = customerArchiveService.getBizCustomerArchive(id);
        return success(BeanUtils.toBean(bizCustomerArchive, CustomerArchiveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户档案分页")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:query')")
    public CommonResult<PageResult<CustomerArchiveRespVO>> getBizCustomerArchivePage(@Valid CustomerArchivePageReqVO pageReqVO) {
        PageResult<CustomerArchiveDO> pageResult = customerArchiveService.getBizCustomerArchivePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerArchiveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户档案 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizCustomerArchiveExcel(@Valid CustomerArchivePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerArchiveDO> list = customerArchiveService.getBizCustomerArchivePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户档案.xls", "数据", CustomerArchiveRespVO.class,
                        BeanUtils.toBean(list, CustomerArchiveRespVO.class));
    }

    @PutMapping("/changeServiceStatus")
    @Operation(summary = "更新客户档案服务状态")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:update')")
    public CommonResult<Boolean> changeServiceStatus(@Valid @RequestBody CustomerArchiveSaveReqVO updateReqVO) {
        customerArchiveService.changeServiceStatus(updateReqVO);
        return success(true);
    }

    @GetMapping("/getSimpleList")
    @Operation(summary = "获得客户档案简易列表")
    @Parameter(name = "ownerUserId", description = "客户所属人id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive:query')")
    public CommonResult<List<Map<String,Object>>> getSimpleList(@Valid CustomerArchivePageReqVO pageReqVO) {
        List<Map<String,Object>> dataList = customerArchiveService.getSimpleList(pageReqVO);
        return success(dataList);
    }

    @GetMapping("/getBizCustomerArchiveList")
    @Operation(summary = "获得客户档案列表-添加公司信息时选择客户用")
    public CommonResult<List<CustomerArchiveDO>> getBizCustomerArchiveList(@Valid CustomerArchivePageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerArchiveDO> list = customerArchiveService.getBizCustomerArchivePage(pageReqVO).getList();
       return success(list);
    }

}